//
//  SinaStatusListViewModel.swift
//  weibo
//
//  Created by sirius on 16/1/6.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit
import AFNetworking

class SinaStatusListViewModel: NSObject {
    
    lazy var list: [SinaStatusViewModel] = [SinaStatusViewModel]()
    func getData(since_id: Int,success:(count:Int)->(),failure:()->()) {
        //创建一个afn 实例
        let afn = AFHTTPSessionManager()
        //拼接参数
        let params = ["access_token":SinaAccountViewModel.shareInstance.access_token,"since_id":since_id]
    
        //发送一个请求
        afn.GET("https://api.weibo.com/2/statuses/home_timeline.json", parameters: params, success: { (_ , JSON) -> Void in
            if let dic = JSON as? [String: AnyObject]{
            //一定要确保有数据，强制解包
                let statues = dic["statuses"] as! [[String: AnyObject]]
                var temp: [SinaStatusViewModel] = [SinaStatusViewModel]()
                
                 for status in statues{
                    let statusModel = SinaStatusModel(dic: status)
                    //添加到数组中
                    temp.append(SinaStatusViewModel(statusModel:statusModel))
                }
                
                /*
                
                当since_id = 0, 直接让 self.list = temp
                当since_id > 0   self.list = temp + self.list
                
                */
                
                if since_id > 0 {
                    // 数据拼接
                    self.list = temp + self.list
                } else {
                    self.list = temp
                }
                success(count: temp.count)
            }
            }) { (_ , error) -> Void in
               printLog(error)
        }
    
    
    
    
    
    }
}
